<?php

namespace app\api\controller\v46;

use app\api\controller\v2\Auth;
use app\common\cachemodel\Orders;
use app\common\cachemodel\zlcr2\Zlcr2UserQuestion;
use app\common\lib\Util;
use app\zyhsapi\model\ZyhsAppFeedback;
use app\zyhsapi\model\ZyhsConsultChatRtcRecord;
use think\response\Json;

class Feedback extends Auth
{
    /**
     * 获取 反馈历史
     * @return Json
     * @throws \Exception
     */
    public function getFeedbackList()
    {
        $minId = input('min_id/d', 0);

        $zyhsAppFeedbackModel = new ZyhsAppFeedback();
        if ($minId === 0 && $zyhsAppFeedbackModel->getHasUnread(2, $this->user['id']))
        {   // 有未读消息 更新至已读
            $zyhsAppFeedbackModel->msgRead(2, $this->user['id']);
        }

        $data = $zyhsAppFeedbackModel->getFeedbackList(2, $this->user, $minId);

        return show(config('code.success'), '请求成功', $data);
    }

    /**
     * 发送反馈消息
     * @return Json
     * @throws \Exception
     */
    public function addFeedbackMsg()
    {
        $content = trim(input('content/s', ''));   // 反馈内容
        $type = input('type/d', 1);
        $phoneInfo = input('phone_info/s', '');
        if (empty($content))
        {
            return show(config('code.error'), '请求异常，反馈内容不能为空～');
        }
        if (!in_array($type, [1, 2, 3, 4], true))
        {
            return show(config('code.error'), '请求异常，反馈类型错误～');
        }
        if (empty($phoneInfo))
        {
            return show(config('code.error'), '请求异常，设备信息不能为空～');
        }

        if (Util::commentCheck($this->user['id'], ['action' => 'addFeedbackMsg', 'type' => $type, 'content' => $content]))
        {
            return show(config('code.error'), '操作频繁，请稍后再试～');
        }
        $extend = [];
        if (in_array($type, [3, 4], true))
        {   // 反馈 服务
            if ($type === 3)
            {   // 连线咨询
                $data = (new ZyhsConsultChatRtcRecord())->getInfoById($content);
                $serType = 100;
                $content = (int)$content;
            } else
            {   // 问答
                $data = (new Orders())->getInfoById($content, ['order_type', 'fid', 'uid']);
                $serType = 22;
                $content = $data['fid'];
            }
            if (!$data || (int)$data['uid'] !== $this->user['id'])
            {
                return show(config('code.error'), '服务异常，反馈服务不存在～');
            }
            $extend = [
                'type' => $serType, // 1=>连线咨询 2=>悬赏问答
                'id' => $content,
            ];
            $content = '';
        }

        $data = [
            'app_type' => 2,
            'uid' => $this->user['id'],
            'type' => 1,
            'content' => $content,
            'content_type' => $type,
            'order_type' => $serType ?? 0,
            'extend' => $extend,
            'app_version' => $this->headers['version'],
            'phone_type' => $this->headers['model'],
            'phone_system' => $this->headers['app-type'] === 'ios' ? 2 : 1,
            'phone_info' => $phoneInfo,
            'is_read' => 0,
            'read_time' => 0,
            'create_time' => $this->request->time(),
        ];

        $zyhsAppFeedbackModel = new ZyhsAppFeedback();
        $id = $zyhsAppFeedbackModel->SaveData($data);
        $data['id'] = $id;
        $resData = current($zyhsAppFeedbackModel->resDataResert([$data], $this->user));
        $resData['time_des'] = '';

        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 获取 服务列表
     * @return Json
     * @throws \Exception
     */
    public function getServiceList()
    {
        $type = input('type/d', 0);
        if (!in_array($type, [1, 2], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }

        if ($type === 1)
        {
            $resData = (new ZyhsConsultChatRtcRecord())->getRtcServiceList(2, $this->user['id']);
        } else
        {
            $zyhsUserQuestionModel = new Zlcr2UserQuestion();
            //状态 0=>待下单 1=>待支付 2=>已支付【待接单】3=>未支付已取消 4=>待解答【已接单】5=>已解答【可追问48小时后自动完成】6=>已完成  7=>已完成且收益扣除 8=>追问待解答 9=>占星师弃答3次取消并退款 10=>用户支付后主动取消并退款  11=>接单超时已取消订单  20=>退回重答 21=>已完成且收益扣除【占星师放弃重答】',
            $qids = $zyhsUserQuestionModel->where(['uid' => $this->user['id'], 'status' => ['in', [2, 4, 5, 6, 7, 8, 20, 21]], 'question_type' => ['in', [1, 2, 3]]])->column('id');

            $page = input('page/d', 1);
            // 统计有多少订单
            $total = count($qids);
            //获取数据分页信息
            $per_page = 10;// 当前订单订单每页显示数量
            $resData = getPaginatorData($total, $page, $per_page);
            //获取分页后的数据
            $resData['data'] = array_slice($qids, $resData['start'], $per_page);
            unset($resData['start']);
            unset($resData['end']);

            $data = $zyhsUserQuestionModel->batchGetInfoByIds($resData['data'], ['id', 'question', 'order_num', 'question_type']);
            $zyhsUserQuestionModel->dataInit($data);
            $res = [];
            $questionTypeArr = [
                1 => '星盘',
                2 => '骰子',
                3 => '幸运卡牌',
            ];
            foreach ($data as $dataItem)
            {
                $questionType = $questionTypeArr[$dataItem['question_type']];
                $res[] = [
                    'id' => $dataItem['order_num'] ?? '',
                    'avatar' => '',
                    'name' => $dataItem['question'],
                    'list' => [
                        [
                            'name' => '服务编号',
                            'value' => $dataItem['order_num'] ?? '',
                        ],
                        [
                            'name' => '问题类型',
                            'value' => $questionType,
                        ],
                    ],
                ];
            }
            $resData['data'] = $res;
        }
        return show(config('code.success'), '请求成功', $resData);
    }
}
